package by.yativem.ins.web.actions;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;



public class LoggerAction extends AbstaractActonSupport {
	
	private static final  org.apache.log4j.Logger log =  org.apache.log4j.Logger.getLogger(LoggerAction.class);
	
	public static class LogEntity implements Serializable{
		
		private String logName;
		private String logLevel;
		
		public String getLogName() {
			return logName;
		}
		public void setLogName(String logName) {
			this.logName = logName;
		}
		public String getLogLevel() {
			return logLevel;
		}
		public void setLogLevel(String logLevel) {
			this.logLevel = logLevel;
		}
	}
	
	private static  List<LogEntity> loggers = new ArrayList<LogEntity>();
	
	private String newLogEntityName;
	private String newLogEntityLevel;
	
	public String execute(){		
			
		//LogManager.getLoggerRepository()
		
		log.info("Logger info");	
		log.debug("Logger debug");
		log.error("Logger error");
		log.trace("Logger trace");
		
		return SUCCESS;
	}
	
	public String add(){
		
		LogEntity le = new LogEntity();
		le.setLogLevel(newLogEntityLevel);
		le.setLogName(newLogEntityName);		
		
		Logger log= LogManager.getLogger( le.getLogName());
		log.setLevel(Level.toLevel( le.logLevel) );
		
		loggers.add(le);
		return SUCCESS;
	}

	public List<LogEntity> getLoggers() {
		return LoggerAction.loggers;
	}

	public void setLoggers(List<LogEntity> loggers) {
		LoggerAction.loggers = loggers;
	}

	public List<String> getLogLevels(){
		return Arrays.asList(
				Level.ALL.toString(),
				Level.TRACE.toString(),
				Level.DEBUG.toString(),
				Level.INFO.toString(),
				Level.WARN.toString(),
				Level.ERROR.toString(),
				Level.OFF.toString()			
				
				);
		
	}

	public String getNewLogEntityName() {
		return newLogEntityName;
	}

	public void setNewLogEntityName(String newLogEntityName) {
		this.newLogEntityName = newLogEntityName;
	}

	public String getNewLogEntityLevel() {
		return newLogEntityLevel;
	}

	public void setNewLogEntityLevel(String newLogEntityLevel) {
		this.newLogEntityLevel = newLogEntityLevel;
	}
	
	
	

}
